class Admin::ChannelsController < AdminController
  def index
    @channels = Channel.all.order("sort ASC").page(params[:page]).per(params[:per])
  end

  def new
    @parent_channel = Channel.find params[:parent_id] if params[:parent_id].presence
    @channel = Channel.new
  end

  def create
    @channel = Channel.new channel_params
    if @channel.save
      redirect_to admin_channels_path, notice: "保存成功！"
    else
      redirect_to admin_channels_path, notice: "保存失败！"
    end
  end

  def edit
    @channel = Channel.find params[:id]
    @parent_channel = @channel.parent if @channel.parent.presence
  end

  def update
    @channel = Channel.find params[:id]
    if @channel.update channel_params
      redirect_to admin_channels_path, notice: "更新成功！"
    else
      redirect_to admin_channels_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @channel = Channel.find params[:id]
    if @channel.destroy
      redirect_to admin_channels_path, notice: "删除成功."
    else
      redirect_to admin_channels_path, notice: "删除失败."
    end
  end

  private

  def channel_params
    params.require(:channel).permit(:name, :sort, :avatar, :published, :page_url, :page_type, :params, :share_avatar, :share_title, :share_content)
  end
end
